package com.szpx.controller.LishihuiController;

import com.alibaba.fastjson.JSONArray;
import com.szpx.entity.Consumer;
import com.szpx.entity.User;
import com.szpx.service.ConsumerService;
import com.szpx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/api")
public class ConsumerController<Login> {

    @Autowired
    private ConsumerService consumerService;
    @Autowired
    private UserService userService;
    public static final  String SESSION_VERIFY_KEY="consumer";
    // 处理注册表单提交


    @ResponseBody
    public String ajaxRequest(@RequestBody String jsonDataFromTheFrontEnd, HttpSession session, Model model) {
        // 接口测试数据：{"cellphone":"17687188086","vercode":"t","password":"123","confirmPassword":"123","nickname":"1","agreement":"on"}
        // 在这里处理接收到的JSON数据，比如解析JSON字符串并保存到数据库中。

        // 为了展示返回的JSON效果，这里直接将前端传过来的String类型的jsonDataFromTheFrontEnd转化为JSON后返回到前端去
        String result = "";
        if (jsonDataFromTheFrontEnd != null || jsonDataFromTheFrontEnd != "") {
            //前端传过来的jsonDataFromTheFrontEnd值是：{"cellphone":"17687188086","vercode":"17687188086","password":"17687188086","confirmPassword":"17687188086","nickname":"17687188086","agreement":"on"}
            System.out.println("前端传过来的jsonDataFromTheFrontEnd值是：" + jsonDataFromTheFrontEnd);
            // 使用alibaba.fastjson对字符串进行反序列化
            // 创建一个包含多个对象的列表
            // 构造多个对象绑定的JSON格式数据,此处借用User的查询业务
            // 当然如果是注册业务，这里应该去执行将Controller中接受的数据存入数据库的操作,此处只做简单展示
            List<User> userList = userService.selectAllUser();
            // 将对象列表转换为 JSON格式的字符串数组并返回
            String jsonArrayString = JSONArray.toJSONString(userList);
            System.out.println("准备返回的JSON字符串数据是：" + jsonArrayString); // 输出 JSON 数组字符串
            System.out.println("successful");
            result = jsonArrayString;
        } else {
            result = "failed";
        }

        return result;// 返回成功消息给前端（仅作为示例）。在实际应用中，你可能需要返回一个包含更多信息的JSON响应对象。
    }




    @RequestMapping("/zhuce")
    public String toFormTableAjaxIndex(HttpSession session) {
        return "lishihuiTeacherHtml/lishihuiAjaxHtml/Consumer";
    }


    @PostMapping("/register")
    public String register(Consumer consumer) {
        User user = new User();
        user.setName(consumer.getUsername());
        user.setEmail(consumer.getEmail());
        user.setPassword(consumer.getPassword());
        userService.addUser(user);
        return "lishihuiTeacherHtml/lishihuiAjaxHtml/login";
    }



    @RequestMapping("/tiaozhuan")
    public String xxx() {

        return "lishihuiTeacherHtml/lishihuiAjaxHtml/login";
    }
}
